package com.tgy.MyFlowable.workflow.controller;

import com.ctsi.core.auth.annotation.PreAuth;
import com.ctsi.core.common.api.Result;
import com.ctsi.workflow.dto.FlwDefinitionDTO;
import com.ctsi.workflow.service.IFlwDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * 流程定义控制类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 20:09
 */
@RestController
@RequestMapping("/flow/definition")
@RequiredArgsConstructor
@Api(tags = "工作流：流程定义")
public class FlwDefinitionController {

    private final IFlwDefinitionService flwDefinitionService;

    @GetMapping("/page")
    @ApiOperation(value = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", required = true, value = "当前页", paramType = "form"),
            @ApiImplicitParam(name = "size", required = true, value = "每页显示数据", paramType = "form"),
            @ApiImplicitParam(name = "keyword", required = true, value = "模糊查询关键词", paramType = "form"),
    })
    public Result<?> page(FlwDefinitionDTO flwDefinitionDTO) {
        return Result.data(flwDefinitionService.page(flwDefinitionDTO));
    }

    @PreAuth
    @ApiOperation(value = "部署")
    @PostMapping("/deploy")
    public Result<?> deploy(@RequestBody FlwDefinitionDTO flwDefinitionDTO) {
        flwDefinitionService.deploy(flwDefinitionDTO);
        return Result.condition(Boolean.TRUE);
    }


    @PreAuth
    @GetMapping("/trace")
    @ApiOperation(value = "追踪")
    public Result<?> trace(FlwDefinitionDTO flwDefinitionDTO) {
        return Result.data(flwDefinitionService.trace(flwDefinitionDTO));
    }

    @PreAuth
    @ApiOperation(value = "导出")
    @GetMapping(value = "/export", produces = MediaType.APPLICATION_XML_VALUE)
    public void export(FlwDefinitionDTO flwDefinitionDTO, HttpServletResponse response) {
        flwDefinitionService.export(flwDefinitionDTO, response);
    }

    @PreAuth
    @PostMapping("/active")
    @ApiOperation(value = "激活")
    public Result<?> active(@RequestBody FlwDefinitionDTO flwDefinitionDTO) {
        flwDefinitionService.activeOrSuspend(flwDefinitionDTO, false);
        return Result.condition(Boolean.TRUE);
    }

    @PreAuth
    @PostMapping("/mapping")
    @ApiOperation(value = "映射")
    public Result<?> mapping(@RequestBody FlwDefinitionDTO flwDefinitionDTO) {
        flwDefinitionService.mapping(flwDefinitionDTO);
        return Result.condition(Boolean.TRUE);
    }

    @PreAuth
    @PostMapping("/suspend")
    @ApiOperation(value = "挂起")
    public Result<?> suspend(@RequestBody FlwDefinitionDTO flwDefinitionDTO) {
        flwDefinitionService.activeOrSuspend(flwDefinitionDTO, false);
        return Result.condition(Boolean.TRUE);
    }

    @PreAuth
    @GetMapping("/user-task")
    @ApiOperation(value = "用户任务节点")
    public Result<?> userTask(FlwDefinitionDTO flwDefinitionDTO) {
        return Result.data(flwDefinitionService.userTask(flwDefinitionDTO));
    }

    @PreAuth
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public Result<?> del(@RequestBody FlwDefinitionDTO flwDefinitionDTO) {
        flwDefinitionService.delete(flwDefinitionDTO);
        return Result.condition(Boolean.TRUE);
    }
}
